package com.combetter.changxing.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.combetter.changxing.R;
import com.combetter.changxing.http.dialog.LoadingDailogs;
import com.combetter.changxing.utils.ScreenUtil;

import butterknife.ButterKnife;


public abstract class BaseFragment extends Fragment {

    protected AppCompatActivity activity;
    private LoadingDailogs loadingDailogs;
    //遇到过哪些关于Fragment的问题,如何处理的?
    //getActivity()空指针:这种情况一般发生在异步任务里调用getActivity(),而Fragment已经onDetach(),此时就会有空指针，
    //解决方案是在Fragment里使用一个全局变量mActivity,在onAttach()方法里赋值，这样可能会引起内存泄漏，但是异步任务没有
    //没有停止的情况下本身就已经可能内存泄漏，相比直接crash,这种方式显得更妥当一些。
    //Fragment视图重叠：在类onCreate()的方法加载Fragment,并且没有判断saveInstanceState==null 或if(findFragmentByTag(mFragmentTag)==null),
    //导致重复加载了同一个Fragment导致重叠。(replace情况下，如果没有加入回退栈，则不判断也不会造成重叠，但还是建议统一判断下)
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        //在页面重启时，Fragment会被保存恢复，而此时再加载Fragment会重复加载,导致重叠;
        // if(savedInstanceState ==null){}
        //或者if(findFragmentByTag(mFragmentTag)==null)
        super.onCreate(savedInstanceState);
        getBundleData(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(layoutId(), container, false);
        ButterKnife.bind(this, view);

        //沉浸式状态栏
        activity = (AppCompatActivity) getActivity();
        if(activity instanceof BaseActivity){
            BaseActivity baseActivity = (BaseActivity) activity;
            if(baseActivity.isOpenStatus()){
                View actionbar = view.findViewById(R.id.actionbar);
                if(actionbar != null){
                    int statusHeight = ScreenUtil.getStatusHeight(activity);
                    if(statusHeight != -1){
                        actionbar.setPadding(0, statusHeight, 0, 0);
                    }
                }
            }
        }




        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        initViews(view);

        loadData();
        Bundle bundle = getArguments();
        if(bundle!=null){
            getBundleData(bundle);
        }
    }


    /**
     * 返回布局id
     */
    protected abstract int layoutId();

    /**
     * 获取上个页面传过来的数据
     */
    protected void getBundleData(Bundle bundle) {
    }



    /**
     * 初始化View（设置控件样式、初始值等）
     */
    protected abstract void initViews(View view);

    /**
     * 获取数据
     */
    protected abstract void loadData();

    /**
     * 启动Activity
     */
    protected void startActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
    }

    /**
     * 启动Activity,带Bundle参数
     */
    protected void startActivity(Class<? extends Activity> cls, @NonNull Bundle bundle) {
        Intent intent = new Intent(getActivity(), cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 启动Activity,带请求码
     */
    protected void startActivityForResult(Class<? extends Activity> cls, int requestCode) {
        Intent intent = new Intent(getActivity(), cls);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 启动Activity,带请求码,带Bundle参数
     */
    protected void startActivityForResult(Class<? extends Activity> cls, int requestCode, @NonNull Bundle bundle) {
        Intent intent = new Intent(getActivity(), cls);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }
    //显示网络加载框
    public void showNetProgessDialog(String text,boolean isback) {
        if (loadingDailogs==null&&activity!=null){
            LoadingDailogs.Builder builder1=new LoadingDailogs.Builder(activity)
                    .setMessage(text)
                    .setCancelable(isback);
            loadingDailogs = builder1.create();
            loadingDailogs.show();
        }else {
            loadingDailogs.show();
        }
    }

    /**
     * 关闭加载框
     */
    public void  loadDismiss(){
        if( loadingDailogs!=null &&  loadingDailogs.isShowing()){
            loadingDailogs.dismiss();
        }
    }
}
